from fastapi import APIRouter
from model import articles,Article,database
from typing import List,Dict

router = APIRouter()


@router.get("/articlelist/", response_model=List[Article])
async def user_list(page: int = None, size: int = None):
    query = articles.select().limit(size).offset((page-1)*size)
    return await database.fetch_all(query)


@router.post("/articles/")
async def create_article(title: str = None,content:str = None):

    query = articles.insert().values(title=title,content=content)
    last_record_id = await database.execute(query)
    return {"id": last_record_id}


@router.get("/get_article/", response_model=Article)
async def get_article(id: int = None):

    query = "select id,title,content from article where id = %s limit 1 " % id
    return await database.fetch_one(query)
